﻿const http = require('http')
const app = http.createServer()
const port = 3030;

// 通信socketIO

let onlineUsers = [];//在线用户
//当前在线人数
let onlineCount = 0;

app.listen(port,()=>{
    console.log(`在${port}端口启动`)
});


const io = require('socket.io')(app);
//连接
io.on("connection",(client)=>{
    console.log(client.id)
    //登录时
    client.on('login',(data)=>{
        console.log("链接成功！！！",data)
        let userData = JSON.parse(data);
        // onlineUsers.forEach(item=>{
        //   if(item === userData.nickName){
              onlineUsers.push({
                  user:userData.nickName,
                  accountNum:userData.accountNum,
                  userID:client.id,
              });
              //上线通知
              client.broadcast.emit('onlineNotification', `${userData.nickName}上线了`);
              onlineCount++;
              console.log(`当前房间人数：${onlineCount}`)
          // }
        // })



    })
    client.on('message', (msg)=>{
        let data = JSON.parse(msg)
        let sendMsg = {
            accountNum: data.accountNum,
            hImage:data.headImages,
            content: data.chatContent,
            chooseRoom:data.chooseRoom,
        }
        console.log("chooseRoom:::",data.chooseRoom.toString())
        let chooseRoom = data.chooseRoom.toString();
        client.emit('sendMessage',sendMsg);
            for(let value of onlineUsers){
               if(chooseRoom.length === 10){
                   console.log("user",value.accountNum)
                   console.log("user2",typeof value.accountNum)
                   console.log("chooseRoom",chooseRoom)
                   console.log("chooseRoom2",value.accountNum === chooseRoom)
                   //私聊
                   if(value.accountNum === chooseRoom){
                       console.log("发送私聊")
                       client.to(value.userID).emit('sendMessage',sendMsg)
                   }
               }else{
                   //群聊
                   console.log("发送群聊",value.userID)
                   client.to(value.userID).emit("sendMessage", sendMsg);
               }
            }
        //在线所有人都可收到
    //     client.broadcast.emit("sendMessage", sendMsg);
        console.log("服务器接收到：",data.chatContent)
    })

    //
    //断开连接
    client.on('outConn', (data)=>{
        data = JSON.parse(data)
        for(let value of onlineUsers){
            console.log("value.accountNum::",value.accountNum)
            console.log(" === data.accountNum::",data.accountNum)
            console.log("value.accountNum === data.accountNum::",value.accountNum === data.accountNum)
            onlineUsers = onlineUsers.filter((value)=>{
                return value.accountNum !== data.accountNum
            })
        }
        --onlineCount;
        console.log(onlineUsers)
        console.log(`断开链接,还有${onlineCount}个在线。`)
        client.emit('disconne',"成功断开连接！")
        client.disconnect(true)

    });

    client.on("error", (error) => (
            {
                result: error + "连接异常",
            }
        )
    )
    client.on('close', function () {
        console.log('客户端下线了');

    });
})



